package io.geekidea.boot.shop.service.business.business;

import io.geekidea.boot.framework.exception.BusinessException;
import io.geekidea.boot.framework.page.OrderByItem;
import io.geekidea.boot.framework.page.OrderMapping;
import io.geekidea.boot.framework.page.Paging;
import io.geekidea.boot.shop.mapper.MemberMapper;
import io.geekidea.boot.shop.query.CustomerQuery;
import io.geekidea.boot.shop.service.base.api.AddressService;
import io.geekidea.boot.shop.service.business.api.ICustomerService;
import io.geekidea.boot.shop.vo.CustomerLevelVo;
import io.geekidea.boot.util.PagingUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * 客户相关
 */
@Service
@Slf4j
public class ICustomerServiceImpl implements ICustomerService {

    @Resource
    private MemberMapper memberMapper;

    @Resource
    private AddressService addressService;

    @Override
    public Paging<CustomerLevelVo> getCustomerLevelInfo(CustomerQuery customerDto) {
        OrderMapping orderMapping = new OrderMapping();
        orderMapping.put("createTime", "create_time");
        PagingUtil.handlePage(customerDto, orderMapping, OrderByItem.desc("id"));
        List<CustomerLevelVo> list = memberMapper.getCustomerLevelInfo(customerDto);
        Paging<CustomerLevelVo> page = new Paging<>(list);
        List<CustomerLevelVo> dealList = page.getList();
        //查默认地址
        dealList.forEach(customerLevelVo -> {
            customerLevelVo.setAddress(addressService.getByMemberId(customerLevelVo.getId()));
        });
        return page;
    }

    @Override
    public Paging<CustomerLevelVo> getInviteList(CustomerQuery customerDto) {
        if (customerDto.getInviteId() == null) {
            throw new BusinessException("客户id不能为空");
        }
        OrderMapping orderMapping = new OrderMapping();
        orderMapping.put("createTime", "create_time");
        PagingUtil.handlePage(customerDto, orderMapping, OrderByItem.desc("id"));
        List<CustomerLevelVo> dealList = memberMapper.getCustomerLevelInfo(customerDto);
        //查询推荐人数 todo
        return new Paging<>(dealList);
    }
}
